home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Add-Ons / After Dark / Joe Judge / Sphere ƒ / (original sphere.c) next >
Encoding:
Text File  |  1994-07-20  |  3.8 KB  |  120 lines  |  [TEXT/KAHL]

  1. #ifndef lint
  2. static char sccsid[] = "@(#)sphere.c 1.3 88/02/26 XLOCK";
  3. #endif
  4.  
  5. /*-
  6.  * sphere.c - Draw a bunch of shaded spheres for xlock,
  7.  * the X Window System lockscreen.
  8.  *
  9.  * Copyright (c) 1988 by Sun Microsystems
  10.  *
  11.  * See xlock.c for copying information.
  12.  *
  13.  * Revision History:
  14.  * 2-Sep-93: xlock version (David Bagley bagleyd@source.asset.com)
  15.  * 1988: Revised to use SunView canvas instead of gfxsw Sun Microsystems
  16.  * 1982: Orignal Algorithm  Tom Duff  Lucasfilm Ltd.
  17.  */
  18.  
  19. /* original copyright
  20.  ******************************************************************************
  21.  Copyright 1988 by Sun Microsystems, Inc. Mountain View, CA.
  22.  
  23.  All Rights Reserved
  24.  
  25.  Permission to use, copy, modify, and distribute this software and its
  26.  documentation for any purpose and without fee is hereby granted,
  27.  provided that the above copyright notice appear in all copies and that
  28.  both that copyright notice and this permission notice appear in
  29.  supporting documentation, and that the names of Sun or MIT not be
  30.  used in advertising or publicity pertaining to distribution of the
  31.  software without specific prior written permission. Sun and M.I.T.
  32.  make no representations about the suitability of this software for
  33.  any purpose. It is provided "as is" without any express or implied warranty.
  34.  
  35.  SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  36.  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  37.  PURPOSE. IN NO EVENT SHALL SUN BE LIABLE FOR ANY SPECIAL, INDIRECT
  38.  OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  39.  OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  40.  OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  41.  OR PERFORMANCE OF THIS SOFTWARE.
  42.  *****************************************************************************/
  43.  
  44. #include <math.h>
  45. #include "xlock.h"
  46. /*
  47.  * (NX, NY, NZ) is the light source vector -- length should be
  48.  * 100
  49.  */
  50. #define NX 48
  51. #define NY (-36)
  52. #define NZ 80
  53. #define NR 100
  54. #define MIN(a,b) (((a)<(b))?(a):(b))
  55. #define SQRT(a) ((int)sqrt((double)(a)))
  56.  
  57. typedef struct {
  58.     int      width, height;
  59.     int      radius;
  60.     int      x0;        /* x center */
  61.     int      y0;        /* y center */
  62.     int      color;
  63.     int      x;
  64.     int      maxy;
  65. } spherestruct;
  66.  
  67. static spherestruct spheres[MAXSCREENS];
  68.  
  69. void
  70. initsphere(win)
  71.     Window        win;
  72. {
  73.     spherestruct *ss = &spheres[screen];
  74.     XWindowAttributes xwa;
  75.  
  76.     XGetWindowAttributes(dsp, win, &xwa);
  77.     ss->width = xwa.width;
  78.     ss->height = xwa.height;
  79.  
  80.     XSetForeground(dsp, Scr[screen].gc, BlackPixel(dsp, screen));
  81.     XFillRectangle(dsp, win, Scr[screen].gc, 0, 0, ss->width, ss->height);
  82.  
  83.     ss->x = ss->radius = 0;
  84. }
  85.  
  86. void
  87. drawsphere(win)
  88.     Window        win;
  89. {
  90.     spherestruct *ss = &spheres[screen];
  91.     register      y;
  92.  
  93.     if (ss->x >= ss->radius) {
  94.     ss->radius = random() % (MIN(ss->width / 2, ss->height / 2) - 1) + 1;
  95.     ss->x0 = random() % ss->width;
  96.     ss->y0 = random() % ss->height;
  97.     ss->x = -ss->radius;
  98.  
  99.         if (Scr[screen].npixels > 2)
  100.             ss->color = random() % Scr[screen].npixels;
  101.     }
  102.     ss->maxy = SQRT(ss->radius * ss->radius - ss->x * ss->x);
  103.     XSetForeground(dsp, Scr[screen].gc, BlackPixel(dsp, screen));
  104.     XDrawLine(dsp, win, Scr[screen].gc,
  105.         ss->x0 + ss->x, ss->y0 - ss->maxy,
  106.         ss->x0 + ss->x, ss->y0 + ss->maxy);
  107.     if (!mono && Scr[screen].npixels > 2)
  108.         XSetForeground(dsp, Scr[screen].gc, Scr[screen].pixels[ss->color]);
  109.     else
  110.         XSetForeground(dsp, Scr[screen].gc, WhitePixel(dsp, screen));
  111.     for (y = -ss->maxy; y <= ss->maxy; y++)
  112.         if ((random() % (ss->radius * NR)) <=
  113.                 (NX * ss->x + NY * y + NZ *
  114.          SQRT(ss->radius * ss->radius - ss->x * ss->x - y * y)))
  115.             XDrawPoint(dsp, win, Scr[screen].gc,
  116.                 ss->x + ss->x0, y + ss->y0);
  117.     ss->x++;
  118. }
  119.  
  120.